package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.citythreelist.ProvinceActivity;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ComInfo extends BaseActivity {
      TextView cfsava;
      ImageView cf_back;
      MyApplication mapp;
      String uid;
      JSONObject Res=null;

      LinearLayout pclin;

    String provinceId="",cityId="",countyId="",provinceName="",cityName="",countyName="";
    EditText comname,dedress,pername,bankname,banknum,pernum;
    TextView comadress;
    String Id="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_com_info);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        iniv();
        getCompy();
        }


    public void iniv(){
        cfsava= (TextView) findViewById(R.id.cfsava);
        cf_back= (ImageView) findViewById(R.id.cf_back);
        pclin= (LinearLayout) findViewById(R.id.pclin);
        comadress=(TextView) findViewById(R.id.comadress);
        comname=(EditText) findViewById(R.id.comname);
        dedress=(EditText) findViewById(R.id.dedress);
        pername=(EditText) findViewById(R.id.pername);
        bankname=(EditText) findViewById(R.id.bankname);
        banknum=(EditText) findViewById(R.id.banknum);
        pernum=(EditText) findViewById(R.id.pernum);
        cfsava.setOnClickListener(this);
        cf_back.setOnClickListener(this);
        pclin.setOnClickListener(this);
        }


    @Override
    protected void onResume() {
        super.onResume();
        if (mapp.isIscity()){
            provinceName=mapp.getProviece().getName();
            cityName=mapp.getCityitem().getName();
            countyName=mapp.getAreaim().getName();
            provinceId=mapp.getProviece().getId();
            cityId=mapp.getCityitem().getId();
            countyId=mapp.getAreaim().getId();
            comadress.setText(provinceName+cityName+countyName);
            mapp.setIscity(false);
           }

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.cf_back:
                finish();
                break;
            case R.id.pclin:
                mapp.setArechose(2);
                //选择地区
                Intent intent=new Intent(ComInfo.this, ProvinceActivity.class);
                startActivity(intent);
                break;

            case R.id.cfsava:
                if (null==Res){
                  addcominf();
                }else {
                  editcominf();
                  }
                break;
            }
          }

     //获取公司信息
       public void getCompy(){
           OkHttpUtils.get(BaseUrl.compony_info)
              .params("userId",uid)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                           try {
                               JSONObject js=new JSONObject(s);
                               boolean State=js.getBoolean("State");
                               if (State){
                                 boolean Flag=js.getBoolean("Flag");
                                 if (Flag){
                                     Res=js.getJSONObject("Res");

                                     Id = Res.getString("Id");
                                     String MemberId=Res.getString("MemberId");
                                     provinceId=Res.getString("ProvinceId");
                                     provinceName=Res.getString("ProvinceName");
                                     cityId=Res.getString("CityId");
                                     cityName=Res.getString("CityName");
                                     countyId=Res.getString("CountyId");
                                     countyName=Res.getString("CountyName");
                                     String CompanyName=Res.getString("CompanyName");
                                     String Address=Res.getString("Address");
                                     String Number=Res.getString("Number");
                                     String Bank=Res.getString("Bank");
                                     String BankAccount=Res.getString("BankAccount");
                                     String TrueName=Res.getString("TrueName");
                                     String Status=Res.getString("Status");
                                     String CreateTime=Res.getString("CreateTime");
                                     comname.setText(CompanyName);
                                     comadress.setText(provinceName+cityName+countyName);
                                     dedress.setText(Address);
                                     pername.setText(TrueName);
                                     bankname.setText(Bank);
                                     banknum.setText(BankAccount);
                                     pernum.setText(Number);

                                 }
                               }
                           } catch (JSONException e) {
                               e.printStackTrace();
                           }
                       }
                       @Override
                       public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                           super.onError(isFromCache, call, response, e);
                       }
                   });
              }

           public void addcominf(){
               String companyName=comname.getText().toString();
               String address=dedress.getText().toString();
               String number=pernum.getText().toString();
               String bank=bankname.getText().toString();
               String bankAccount=banknum.getText().toString();
               String trueName=pername.getText().toString();

               OkHttpUtils.post(BaseUrl.compony_addinfo)
                     .params("userId",uid)
                     .params("provinceId",provinceId)
                     .params("provinceName",provinceName)
                     .params("cityId",cityId)
                     .params("cityName",cityName)
                     .params("countyId",countyId)
                     .params("countyName",countyName)
                     .params("companyName",companyName)
                     .params("address",address)
                     .params("number",number)
                     .params("bank",bank)
                     .params("bankAccount",bankAccount)
                     .params("trueName",trueName)
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             System.out.println("----------->添加公司信息===="+s);
                             try {
                                 JSONObject js=new JSONObject(s);
                                 boolean State=js.getBoolean("State");
                                 if (State){

                                     finish();
                                 }
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                         @Override
                         public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                             super.onError(isFromCache, call, response, e);
                         }
                     });
                   }

  //编辑公司信息
    public void editcominf(){
        String companyName=comname.getText().toString();
        String address=dedress.getText().toString();
        String number=pernum.getText().toString();
        String bank=bankname.getText().toString();
        String bankAccount=banknum.getText().toString();
        String trueName=pername.getText().toString();
        OkHttpUtils.post(BaseUrl.compony_editinfo)
                .params("id",Id)
                .params("provinceId",provinceId)
                .params("provinceName",provinceName)
                .params("cityId",cityId)
                .params("cityName",cityName)
                .params("countyId",countyId)
                .params("countyName",countyName)
                .params("companyName",companyName)
                .params("address",address)
                .params("number",number)
                .params("bank",bank)
                .params("bankAccount",bankAccount)
                .params("trueName",trueName)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);

                            boolean State=js.getBoolean("State");
                            if (State){
                            boolean Flag=js.getBoolean("Flag");
                            if (Flag){
                                finish();
                            }else {
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(ComInfo.this,Msg);
                              }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
       }


}
